var viewerFriends;
		var flexAppId = "OpenSocialFlex";
		gadgets.util.registerOnLoadHandler(init);
		
			function init()
			{
				loadFlash();
				
			}
		 /**
		  * Loads the flash stuff from App Engine
		  */
			function loadFlash()
			{
				var embedFlashHtml = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
				+ 'id="OpenSocialWrapper" width="600" height="283"'
				+ 'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'
				+ '<param name="movie" value="http://yellowbird.googlecode.com/svn/trunk/OpensocialGadgets/gadgets/gadget.swf" />'
				+ '<param name="flashvars" value="bridgeName=b_gadget"/>'
				+ '<param name="quality" value="high" />'
				+ '<param name="allowScriptAccess" value="always" />'
				+ '<embed src="http://yellowbird.googlecode.com/svn/trunk/OpensocialGadgets/gadgets/gadget.swf" quality="high"'
				+ 'width="600" height="283" name="OpenSocialFlex"' 
				+ 'align="middle"'
				+ 'play="true"'
				+ 'loop="false"'
				+ 'quality="high"'
				+ 'allowScriptAccess="always"'
				+ 'type="application/x-shockwave-flash"'
				+ 'pluginspage="http://www.adobe.com/go/getflashplayer"'
				+ 'flashvars="bridgeName=b_gadget">'
				+ '</embed>'
				+ '</object>';
				
				var flashcontainer = document.getElementById('flashcontainer');
				flashcontainer.innerHTML = embedFlashHtml;
				
				//gadgets.util.adjustHeight();
				_IG_AdjustIFrameHeight()
			}
			
			
		function getFriends(){
		  getData();
		  }
		 /**
		  * Request for friend information.
		  */
		  function getData() {    
		    var req = opensocial.newDataRequest();       
		    req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.VIEWER), 'viewer');
		    req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS), 'viewerFriends');
		    req.send(onLoadFriends);   
		    
		  };
		
		 /**
		  * Parses the response to the friend information request and generates
		  * html to list the friends along with their display name.
		  *
		  * @param {Object} dataResponse Friend information that was requested.
		  */
		  function onLoadFriends(dataResponse) {
		    var viewer = dataResponse.get('viewer').getData();
		    var html = 'Friends of ' + viewer.getDisplayName(); 
		    html += ':<br><ul>';
		    viewerFriends = dataResponse.get('viewerFriends').getData();
		    viewerFriends.each(function(person) {
		      html += '<li>' + person.getDisplayName() + ' </li>';
		    });
		    html += '</ul>';
		    var friends = new Array();
			addFriends(viewerFriends,friends);
	 		setFriends(friends);
		    
		
		  };
		  
		  
		  /**
		   * For sending inivitation to viewer friends.
		   *
		   */
		   function sendInivitaion(){
		          var opts = new Object();
		          var messageSubject = "Yons Playstation Portable quiz game";
		          var messageBody = "Win a chance to get Ynos Playstation Portable by playing this Game inside Ynos PSP";
		          
		          opts[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;
		          opts[opensocial.Message.Field.TITLE] = "Title of message"; 
		          var message = opensocial.newMessage(messageBody, opts);
		         opensocial.requestShareApp(opensocial.DataRequest.Group.VIEWER_FRIENDS, message,callbackInivitation);

		  
		   }
		      
		  /**
		   * Callback method to handle the sendInivitation method.
		   */
		   function callbackInivitation(status) {
		    	if (status.hadError()){    	
					//alert(status.getErrorMessage());
		       		//alert("operation failed.");
		       		sendMessage();
		    	}

		  };
		   
		  /**
		   * send message to view friends.
		   *
		   */
		   function sendMessage(){  		
		   		
		   		
		   		var opts = new Object();
		          var messageBody = "Win a chance to get Ynos Playstation Portable by playing this Game inside Ynos PSP";
		          opts[opensocial.Message.Field.TYPE] = opensocial.Message.Type.NOTIFICATION;
		          opts[opensocial.Message.Field.TITLE] = "Yons Playstation Portable quiz game"; 
		          var message = opensocial.newMessage(messageBody, opts); 		
		          
		        opensocial.requestSendMessage(opensocial.DataRequest.Group.VIEWER_FRIENDS, message,messageCallback);

		       
		   }
		 
		  /**
		   * Callback method to handle the sendMessage method.
		   */
		  function messageCallback(status) {
		    if (status.hadError())
		    {
			//alert(status.getErrorMessage());
		

		    }

		  };
		    /**
		   * adds two title and friend tumbnails to friend object
		   *
		   */
		  	 function makeFriendObject(friend)
			{
					return {
						displayName: friend.getDisplayName(),
						thumbnail: friend.getField(opensocial.Person.Field.THUMBNAIL_URL)
					};
			}
		  /**
		   * builds an arraycollection of friend objects from viewerFriends
		   * @param source viewerFriends
		   * @param dest friend
		   */
			function addFriends(source,dest)
			{
					source.each(function(friend) {
						dest.push(makeFriendObject(friend));
					});
			}
		  /**
		   * calls the actionscript function setFriends
		   * @param friend data containing thumbnail and title
		   */
			function setFriends(friends)
			{
				
					try {
						flex_gadget = FABridge.b_gadget.root();
						flex_gadget.setFriends(friends);	
					} catch(e) {
						//alert(e.toString());
					}
			}
		  /**
		   * test function to check flex ajax bridge
		   */
			function alertJs(param) {
				//alert(param);
			}